package org.abettor.pushbox.view;

import org.abettor.pushbox.map.Boxmap;
import org.abettor.pushbox.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;

/**
 * 绘制地图的类,为了做到统一图片和缩略图的绘画方法
 * 
 * @author jzh
 * 
 */
public class BoxMapImageDraw {
	private Bitmap box, wall, boxOnDest, manDown, manLeft, manRight, manUp,
			dest, backGround, blank;
	private Context context = null;
	private float clipWidth, clipHeight;// 宽度和高度

	public BoxMapImageDraw(Context context) {
		this.context = context;
		box = readBitmapFromResource(R.drawable.box_green);
		wall = readBitmapFromResource(R.drawable.wall);
		boxOnDest = readBitmapFromResource(R.drawable.box_red);
		manDown = readBitmapFromResource(R.drawable.man_down);
		manUp = readBitmapFromResource(R.drawable.man_up);
		manLeft = readBitmapFromResource(R.drawable.man_left);
		manRight = readBitmapFromResource(R.drawable.man_right);
		backGround = readBitmapFromResource(R.drawable.aisle);
		dest = readBitmapFromResource(R.drawable.target);
		blank = readBitmapFromResource(R.drawable.blank);
		clipWidth = box.getWidth();
		clipHeight = box.getHeight();
	}

	// 读取各个资源文件
	private Bitmap readBitmapFromResource(int id) {
		Bitmap bit = ((BitmapDrawable) context.getResources().getDrawable(id))
				.getBitmap();
		return bit;
	}

	/**
	 * 绘制图像
	 * 
	 * @param c1
	 * @param x
	 * @param y
	 * @param clipWidth
	 * @param clipHeight
	 * @param map
	 */
	public void drawEveryClip(Canvas c1, int x, int y, Boxmap map) {
		float begin_x = x * clipWidth;
		float begin_y = y * clipHeight;
		// 内部空节点,目标节点先设背景色
		if (map.isEmpty(x, y) && map.isInnerEmpty(x, y)) {
			c1.drawBitmap(backGround, begin_x, begin_y, new Paint());
		} else if (map.isBrick(x, y)) {// 设置砖头
			c1.drawBitmap(wall, begin_x, begin_y, new Paint());
		} else if (map.isBox(x, y)) {// 设置箱子
			if (map.isDest(x, y)) {// 设置红箱子
				c1.drawBitmap(boxOnDest, begin_x, begin_y, new Paint());
			} else {// 设置普通箱子
				c1.drawBitmap(box, begin_x, begin_y, new Paint());
			}
		} else if (map.isDest(x, y)) {// 是目地节点,先画背景图,在画目的节点
			c1.drawBitmap(backGround, begin_x, begin_y, new Paint());
			c1.drawBitmap(dest, begin_x, begin_y, new Paint());
			if (map.isPerson(x, y)) {
				drawPerson(c1, begin_x, begin_y, new Paint(), map);
			}
		} else if (map.isPerson(x, y)) {
			c1.drawBitmap(backGround, begin_x, begin_y, new Paint());
			drawPerson(c1, begin_x, begin_y, new Paint(), map);
		}
	}

	/**
	 * 绘制每个图像,但没有背景
	 * 
	 * @param c1
	 * @param x
	 * @param y
	 * @param map
	 */
	public void drawEveryClipWithOutBack(Canvas c1, int x, int y, Boxmap map) {
		float begin_x = x * clipWidth;
		float begin_y = y * clipHeight;
		// 内部空节点,目标节点先设背景色
		if (map.isEmpty(x, y)) {
			c1.drawBitmap(blank, begin_x, begin_y, new Paint());
		} else if (map.isBrick(x, y)) {// 设置砖头
			c1.drawBitmap(wall, begin_x, begin_y, new Paint());
		} else if (map.isBox(x, y)) {// 设置箱子
			if (map.isDest(x, y)) {// 设置红箱子
				c1.drawBitmap(boxOnDest, begin_x, begin_y, new Paint());
			} else {// 设置普通箱子
				c1.drawBitmap(box, begin_x, begin_y, new Paint());
			}
		} else if (map.isDest(x, y)) {// 是目地节点,先画背景图,在画目的节点
			c1.drawBitmap(blank, begin_x, begin_y, new Paint());
			c1.drawBitmap(dest, begin_x, begin_y, new Paint());
			if (map.isPerson(x, y)) {
				drawPerson(c1, begin_x, begin_y, new Paint(), map);
			}
		} else if (map.isPerson(x, y)) {
			c1.drawBitmap(blank, begin_x, begin_y, new Paint());
			drawPerson(c1, begin_x, begin_y, new Paint(), map);
		}
	}

	/**
	 * 设置背景色
	 * 
	 * @param c1
	 * @param x
	 * @param y
	 * @param map
	 */
	public void drawBackGroudClip(Canvas c1, int x, int y, Paint paint) {
		float begin_x = x * clipWidth;
		float begin_y = y * clipHeight;
		c1.drawBitmap(blank, begin_x, begin_y, paint);
	}

	// 绘制僵尸的图
	protected void drawPerson(Canvas c1, float begin_x, float begin_y,
			Paint paint, Boxmap map) {
		if (map.isPersonDown()) {
			c1.drawBitmap(manDown, begin_x, begin_y, new Paint());
		} else if (map.isPersonLeft()) {
			c1.drawBitmap(manLeft, begin_x, begin_y, new Paint());
		} else if (map.isPersonRight()) {
			c1.drawBitmap(manRight, begin_x, begin_y, new Paint());
		} else if (map.isPersonUp()) {
			c1.drawBitmap(manUp, begin_x, begin_y, new Paint());
		}
	}

	public float getClipWidth() {
		return clipWidth;
	}

	public float getClipHeight() {
		return clipHeight;
	}

}
